package com.tea.controller;

import com.github.pagehelper.PageInfo;
import com.tea.Model.dto.GoodsAddDTO;
import com.tea.Model.dto.GoodsOrderUpdateDTO;
import com.tea.Model.dto.GoodsPageDTO;
import com.tea.Model.dto.GoodsUpdateDTO;
import com.tea.Model.vo.GoodsDetailsVO;
import com.tea.Model.vo.GoodsPageVO;
import com.tea.service.GoodsService;
import com.tea.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 商品管理
 * */
@RestController
@RequestMapping("/goods")
@Slf4j
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    /**
     * 列表查询
     */
    @GetMapping("/list")
    public Result<Object> list(@RequestParam(defaultValue = "1") Integer pageNumber,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               GoodsPageDTO goodsPageDTO) {
        PageInfo<GoodsPageVO> pageInfo = goodsService.queryListWithPage(pageNumber, pageSize, goodsPageDTO);
        return Result.success(pageInfo);
    }

    /**
     * 商品详情
     */
    @GetMapping("/{id}")
    public Result<GoodsDetailsVO> getGoodsById(@PathVariable Integer id) {
        GoodsDetailsVO goods = goodsService.getDetails(id);
        return Result.success(goods);
    }

    /**
     * 添加商品
     */
    @PostMapping("/add")
    public Result<Object> add(@RequestBody @Valid GoodsAddDTO goods)  {
        try {
            log.info("添加商品，请求：name={}, categoryId={}, specCount={}, coverPresent={}",
                    goods.getName(), goods.getCategoryId(),
                    goods.getSpecList() != null ? goods.getSpecList().size() : 0,
                    goods.getCoverImage() != null && !goods.getCoverImage().isEmpty());
        } catch (Exception ignore) {}
        return goodsService.add(goods);
    }

    /**
     * 商品信息更新
     */
    @PostMapping("/update")
    public Result<Object> release(@RequestBody @Valid GoodsUpdateDTO goodsUpdateDTO) {
        try {
            log.info("更新商品，请求：id={}, name={}, status={}, specCount={}, coverPresent={}",
                    goodsUpdateDTO.getId(), goodsUpdateDTO.getName(), goodsUpdateDTO.getStatus(),
                    goodsUpdateDTO.getSpecList() != null ? goodsUpdateDTO.getSpecList().size() : 0,
                    goodsUpdateDTO.getCoverImage() != null && !goodsUpdateDTO.getCoverImage().isEmpty());
        } catch (Exception ignore) {}
        return goodsService.updateById(goodsUpdateDTO);
    }

    /**
     * 删除商品
     */
    @DeleteMapping("/delete")
    public Result<Object> delete(@RequestBody List<Integer> ids) {
        try { log.info("删除商品，请求：idsCount={}", ids != null ? ids.size() : 0); } catch (Exception ignore) {}
        return goodsService.deleteByIds(ids);
    }

    /**
     * 批量更新商品显示顺序
     */
    @PostMapping("/updateOrder")
    public Result<Object> updateGoodsOrder(@RequestBody @Valid GoodsOrderUpdateDTO goodsOrderUpdateDTO) {
        try { log.info("更新商品显示顺序，请求：itemsCount={}", goodsOrderUpdateDTO.getItems() != null ? goodsOrderUpdateDTO.getItems().size() : 0); } catch (Exception ignore) {}
        return goodsService.updateGoodsOrder(goodsOrderUpdateDTO);
    }

    /**
     * 商品上架/下架
     * @param id 商品ID
     * @param status：1-上架，0-下架
     */
    @PostMapping("/status")
    public Result<Object> updateGoodsStatus(@RequestParam Long id,  @RequestParam Integer status) {
        log.info("更新商品状态，请求：id={}, status={}", id, status);
        return goodsService.updateGoodsStatus(id,status);
    }


}
